﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Web.Library.DomainObject.SQLServer;

namespace Web.Library.Facade.SQLServer
{
    public partial class SysApplicationFacade
    {
        public string ListGroupAuthorizationMenu(int GroupID)
        {
            string str1 = string.Empty;
            try
            {
                //MenuDAL menuDal = new MenuDAL();
                //Collection<Menu> collection1 = new Collection<Menu>();
                //Collection<Menu> collection2 = menuDal.RetrieveMenuByParentID(0);
                List<SysApplication> collection2 = dal.GetList();
                if (collection2.Count > 0)
                {
                    string str2 = "[";
                    List<string> list1 = new List<string>();
                    foreach (SysApplication menu in collection2)
                    {
                        string str3 = string.Empty;
                        List<string> list2 = new List<string>();
                        //GroupAuthorization groupAuthorization1 = new GroupAuthorization();
                        SysGroupAuthorization groupAuthorization1 = new SysGroupAuthorization();
                        //GroupAuthorization groupAuthorization2 = new GroupAuthorizationDAL().RetrieveAuthorizationByGroupIDAndMenuID(GroupID, menu.ID);

                        SysAppGroupAuthorization groupAuthorization2 = dal.RetrieveAuthorizationByGroupIDAndMenuID(GroupID, menu.AppID);
                        list2.Add("\"ID\":" + menu.AppID.ToString());
                        if(groupAuthorization2!=null)
                            list2.Add("\"CheckBox\": \"<input type='checkbox' value='" + menu.AppID.ToString() + "' name='MenuID' " +  (string)(groupAuthorization2.AppAuthID > 0 ? (object)"checked='checked'" : (object)"") + " />\"");
                        else
                            list2.Add("\"CheckBox\": \"<input type='checkbox' value='" + menu.AppID.ToString() + "' name='MenuID' " + " />\"");

                        list2.Add("\"Control\": \"<a href='#' class='editGrid' rel='" + menu.AppID.ToString() + "' title='Edit'></a>&nbsp;<a href='#' class='deleteGrid' rel='" + menu.AppID.ToString() + "' title='Delete'></a>\"");
                        list2.Add("\"Title\":\"" + menu.TitleApp + "\"");
                        if (groupAuthorization2 != null)
                            list2.Add("\"AllowDisplay\": \"<input type='checkbox' value='" + menu.AppID.ToString() + "' name='AllowDisplay' " + (string)(groupAuthorization2.AllowDisplay.ToString().ToLower() == "true" ? "checked='checked'" : (object)"") + " />\"");
                        else
                            list2.Add("\"AllowDisplay\": \"<input type='checkbox' value='" + menu.AppID.ToString() + "' name='AllowDisplay' " + " />\"");

                        
                        list1.Add("{" + string.Join(",", (IEnumerable<string>)list2) + "}");
                    }
                    str1 = str2 + string.Join(",", (IEnumerable<string>)list1) + "]";
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return str1;
        }

        public virtual List<SysApplication> GetListByGroupAppID(string key)
        {
            return dal.GetListByGroupAppID(key);
        }

        public bool UpdateWithoutImage(SysApplication domain)
        {
            return dal.UpdateWithoutImage(domain);
        }
    }
}
